package com.myadapter;

import java.util.List;

import com.bean.News;
import com.example.lizhiinformation.R;
import com.tools.ImageLoader;
import com.tools.Path_URL;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FrontPageadapter extends BaseAdapter {
	private Context context;
	private List<News> newsList;
	private View view;
	private LayoutInflater inflater;

	private final static int FRONTPAGE_MAP = 0;
	private final static int FRONTPAGE_CATEGORY = 1;

	public FrontPageadapter(Context context, List<News> newsList, View view) {
		super();
		this.context = context;
		this.newsList = newsList;
		this.view = view;
		inflater = LayoutInflater.from(context);
	}

	public void addData(List<News> newsList) {
		this.newsList.addAll(newsList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return newsList.size();
	}

	@Override
	public String getItem(int position) {
		// TODO Auto-generated method stub
		return newsList.get(position - 2).getOid()+":"+newsList.get(position-2).getData().getFormat();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position - 2;
	}

	@Override
	public int getViewTypeCount() {

		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		News news = newsList.get(position);
		if (news.getCategory().equals("map")) {
			return FRONTPAGE_MAP;
		} else {
			return FRONTPAGE_CATEGORY;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		switch (getItemViewType(position)) {

		case FRONTPAGE_CATEGORY:
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.fromtpage_list_category, null);
				holder = new ViewHolder();
				holder.textview1 = (TextView) convertView
						.findViewById(R.id.fromentpage_list_category_textView1);
				holder.textview2 = (TextView) convertView
						.findViewById(R.id.fromentpage_list_category_textView2);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.imagetView = (ImageView) convertView
						.findViewById(R.id.fromentpage_list_category_imageview1);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			News news = newsList.get(position);
			holder.textview1.setText(news.getData().getSubject());
			holder.textview2.setText(news.getData().getSummary());
			if (news.getData().getFormat().equals("topic")) {
				holder.title.setText("专题");
				holder.title.setBackgroundColor(Color.RED);
			} else {
				
				if (news.getData().getFormat().equals("advert")) {
					holder.title.setText("活动");
					holder.title.setBackgroundColor(Color.RED);
				} else {

					if (news.getData().getFormat().equals("refute")) {
						holder.title.setText("真相");
						holder.title.setBackgroundColor(Color.RED);
					}else{
						if (news.getData().getFormat().equals("original")) {
							holder.title.setText("独家");
							holder.title.setBackgroundColor(Color.RED);
						}else{
							if(news.getData().getFormat().equals("video")){
								holder.title.setText("视频");
								holder.title.setBackgroundColor(Color.RED);
							}
							holder.title.setText("");
							holder.title.setBackgroundColor(Color.WHITE);
						}
					}
				}
				
			}

			ImageLoader.cachr(context, Path_URL.IMAGEURL
					+ news.getData().getCover(), holder.imagetView, view);
			break;
		case FRONTPAGE_MAP:
			ViewHolderMap holderMap = null;
			if (convertView == null) {

				holderMap = new ViewHolderMap();
				convertView = inflater.inflate(R.layout.fromentpage_list_map,
						null);
				holderMap.image1 = (ImageView) convertView
						.findViewById(R.id.fromtpage_list_map_imageview1);
				holderMap.image2 = (ImageView) convertView
						.findViewById(R.id.fromtpage_list_map_imageview2);
				holderMap.image3 = (ImageView) convertView
						.findViewById(R.id.fromtpage_list_map_imageview3);
				holderMap.textmap = (TextView) convertView
						.findViewById(R.id.fromentpage_list_textview);
				convertView.setTag(holderMap);
			} else {
				holderMap = (ViewHolderMap) convertView.getTag();
			}
			News news1 = newsList.get(position);
			holderMap.textmap.setText(news1.getData().getSubject());
			ImageLoader.cachr(context, Path_URL.IMAGEURL
					+ news1.getData().getPics().get(0).getPhoto(),
					holderMap.image1, view);
			ImageLoader.cachr(context, Path_URL.IMAGEURL
					+ news1.getData().getPics().get(1).getPhoto(),
					holderMap.image2, view);
			ImageLoader.cachr(context, Path_URL.IMAGEURL
					+ news1.getData().getPics().get(2).getPhoto(),
					holderMap.image3, view);

			break;
		default:
			break;
		}
		return convertView;
	}

	class ViewHolder {
		private TextView textview1;
		private TextView textview2;
		private TextView title;
		private ImageView imagetView;

	}

	static class ViewHolderMap {
		private ImageView image1;
		private ImageView image2;
		private ImageView image3;
		private TextView textmap;
	}
}
